import { PropsWithChildren } from 'react'
import { useLaunch } from '@tarojs/taro'
import utils from './utils'
import { setToken } from './stores/useTokenStore'
import './app.scss'

function App({ children }: PropsWithChildren<any>) {
  useLaunch((options: Taro.getLaunchOptionsSync.LaunchOptions) => {
    // 静默登录
    if (process.env.TARO_ENV === 'weapp') {
      silentLogin(options)
    }
  })

  // children 是将要会渲染的页面
  return children
}

const silentLogin = async (_options) => {
  // get 登录凭证
  const { code } = await utils.$taroApis.login()
  // get token
  const { data } = await utils.$apis.auth({ code })
  setToken(data.token)
}


export default App
